//
//  HomeViewController.m
//  项目框架 - by Candy
//
//  Created by Candy on 17/1/5.
//  Copyright © 2017年 Candy. All rights reserved.
//

#import "HomeViewController.h"

#import <PLMediaStreamingKit/PLMediaStreamingKit.h>

@interface HomeViewController ()

@property (nonatomic, strong) PLMediaStreamingSession *session;


@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // streamJSON是从服务端拿回的
    
     //从服务端拿回的streamJSON结构如下：
//    @{@"id": @"stream_id",
//      @"title": @"stream_title",
//      @"hub": @"hub_id",
//      @"publishKey": @"publish_key",
//      @"publishSecurity": @"dynamic", // or static
//      @"disabled": @(NO),
//      @"profiles": @[@"480p", @"720p"],    // or empty Array []
//      @"hosts": @{
//              
//              }
//      };
    NSDictionary *streamJSON = @{ // 这里按照之前从服务端SDK中创建好的stream json结构填写进去
//                                 @"id": @"stream_id",
//                                 @"title": @"stream_title",
//                                 @"hub": @"90candy",
//                                 @"publishKey": @"publish_key",
//                                 @"publishSecurity": @"dynamic",
//                                 @"disabled": @(NO),
//                                 @"profiles": @[@"480p", @"720p"],
//                                 @"hosts": @{
//                                         @"publish": @{
//                                                 @"rtmp": TL_RTMP_URL
//                                                 },
//                                         @"play": @{
//                                                 @"rtmp": LL_RTMP_URL,
//                                                 @"hls": LL_HLS_URL
//                                                 }
//                                         }
                                 };
    PLStream *stream = [PLStream streamWithJSON:streamJSON];

    //创建视频和音频的采集和编码配置对象
    PLVideoCaptureConfiguration *videoCaptureConfiguration = [PLVideoCaptureConfiguration defaultConfiguration];
    PLAudioCaptureConfiguration *audioCaptureConfiguration = [PLAudioCaptureConfiguration defaultConfiguration];
    PLVideoStreamingConfiguration *videoStreamingConfiguration = [PLVideoStreamingConfiguration defaultConfiguration];
    PLAudioStreamingConfiguration *audioStreamingConfiguration = [PLAudioStreamingConfiguration defaultConfiguration];

    //创建推流 session 对象
    self.session = [[PLCameraStreamingSession alloc] initWithVideoCaptureConfiguration:videoCaptureConfiguration audioCaptureConfiguration:audioCaptureConfiguration videoStreamingConfiguration:videoStreamingConfiguration audioStreamingConfiguration:audioStreamingConfiguration stream:stream videoOrientation:AVCaptureVideoOrientationPortrait];

    //将预览视图添加为当前视图的子视图
    [self.view addSubview:self.session.previewView];
    
#pragma mark -- 添加推流操作
    // 一个简单的场景即点击一个按钮，然后触发发起直播的操作。
    //添加触发按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"start" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 100, 44);
    button.center = CGPointMake(CGRectGetMidX([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds) - 80);
    [button addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    
}

//实现按钮事件
- (void)actionButtonPressed:(id)sender {
//    [self.session startWithCompleted:^(BOOL success) {
//        if (success) {
//            NSLog(@"Streaming started.");
//        } else {
//            NSLog(@"Oops.");
//        }
//    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
